
symbols = ["+", "-", "*", "/"]  # 存放运算符
cardValue = []  # 保存结果打印信息
visit = [True, True, True,True]#用于递归标记
temp = ["" for x in range(0, 4)]#初始化为空的数组
res=[]

def dfs(position,arr):#求全排列
    if position == len(arr):#当前长度等于数组长度
        res.append(temp[:])#加入结果
        return#递归结束
    for index in range(0,len(arr)):
        if visit[index] == True:
            temp[position] = arr[index]#赋值
            visit[index] = False#标记
            dfs(position + 1,arr)#递归#返回后解除
            visit[index] = True#解除标记


# 发牌器
def card_fun():
    print("请输入4个数字：")
    kkk = input().split()
    arr=[]#存输入的数字
    for i in kkk:
        i=int(i)#字符串转数字
        arr.append(i)
    dfs(0,arr)#求全排列
    xxx=[]
    for i in res:
        i=tuple(i)#把每个结果转化为元组
        xxx.append(i)
    list_set=xxx[:]#转化为列表后对去重
    list_set=list(set(list_set))
    return list_set  # 存放A(4,4)种排列方式的列表


# 计算方法
cardList = card_fun()  # 将生成的四张牌所有排列顺序放入cardList中

def card_compute():
    for m in range(len(cardList)):
        card_list = cardList[m]#全排列的数
        card1 = card_list[0]
        card2 = card_list[1]
        card3 = card_list[2]
        card4 = card_list[3]
        flag = False
        try:#避免0做分母
            for s1 in symbols:
                result1 = 0
                if s1 == "+":
                    result1 = card1 + card2
                elif s1 == "-":
                    result1 = card1 - card2
                elif s1 == "*":
                    result1 = card1 * card2
                elif s1 == "/":
                    result1 = card1 / card2
                for s2 in symbols:
                    result2 = 0
                    if s2 == "+":
                        result2 = result1 + card3
                    elif s2 == "-":
                        result2 = result1 - card3
                    elif s2 == "*":
                        result2 = result1 * card3
                    elif s2 == "/":
                        result2 = result1 / card3
                    for s3 in symbols:
                        result3 = 0
                        result_else = 0 #前半部分和后半部分的结果，针对有括号的情况
                        if s3 == "+":
                            result3 = result2 + card4
                            result_else = card3 + card4
                        elif s3 == "-":
                            result3 = result2 - card4
                            result_else = card3 - card4
                        elif s3 == "*":
                            result3 = result2 * card4
                            result_else = card3 * card4
                        elif s3 == "/":
                            result3 = result2 / card4
                            result_else = card3 / card4

                        # 判断最终结果是否为24
                        if result3 == 24:#顺序
                            cardValue.append(
                                "((%s %s %s) %s %s ) %s %s = 24" % (card1, s1, card2, s2, card3, s3, card4))
                            flag = True
                        # 括号与括号的运算
                        elif result3 != 24:
                            for s4 in symbols:
                                result3 = 0
                                if s4 == "+":
                                    result3 = result1 + result_else
                                elif s4 == "-":
                                    result3 = result1 - result_else
                                elif s4 == "*":
                                    result3 = result1 * result_else
                                elif s4 == "/":
                                    result3 = result1 / result_else
                                if result3 == 24:
                                    cardValue.append("(%s %s %s) %s (%s %s %s) = 24" % (
                                        card1, s1, card2, s4, card3, s3, card4))
                                    flag = True
                                if flag:
                                    break
                        # 如果得到结果，就退出3次运算的循环
                        if flag:
                            break

        except ZeroDivisionError:
            pass

    card_result = set(cardValue)
    return card_result
# 执行主体

Compute = card_compute()
print("这组卡牌共有 %s 种算法" % (len(Compute)))
print("---" * 10)
for i in Compute:
    print(i)
